import {
  queryTableDataListReq,
  queryTableDataInfoReq,
  addDictionaryTypeReq,
  delDictionaryTypeReq,
  updateDictionaryTypeReq,
  refreshListReq,
} from "./service";
import { message } from "antd";
export default {
  namespace: "SystemDictionary",
  state: {
    init: false,
    pageNumber: 1,
    dictInfo: [],
    formParams: {},
  },
  // 异步获取
  effects: {
    *pageInit({}, { put }: any) {
      yield put({ type: "queryTableDataList" });
    },
    //字典管理列表，页码处理
    *queryTableDataList({ params }: any, { put, select }: any) {
      const { formParams } = yield select(
        (state: any) => state.SystemDictionary
      );
      let _params = {
        pageSize: 10,
      };
      Object.assign(
        _params,
        params?.pageNum && formParams,
        formParams?.pageSize && { pageSize: formParams.pageSize },
        formParams?.pageNum && { pageNum: formParams.pageNum },
        params,
        !params?.pageNum && { pageNum: 1 }
      );
      const { code, msg, rows, total } = yield queryTableDataListReq(_params);
      if (code === 200) {
        yield put({
          type: "save",
          payload: {
            rows,
            total,
            formParams: _params,
            pageNumber: params?.pageNum,
            pageSize: params?.pageSize,
          },
        });
      } else {
        message.error(msg);
      }
    },
    //增加
    *addDictionaryType({ params, callback }: any, { call }: any) {
      const { code, msg } = yield call(addDictionaryTypeReq, params);
      if (code === 200) {
        // eslint-disable-next-line @typescript-eslint/no-unused-expressions
        message.success(msg);
        callback && callback();
      } else {
        message.error(msg);
      }
    },
    //删除
    *delDictionaryType({ params, callback }: any, { call }: any) {
      const { code } = yield call(delDictionaryTypeReq, params);
      if (code === 200) {
        // eslint-disable-next-line @typescript-eslint/no-unused-expressions
        callback && callback();
      }
    },
    //id获取详情
    *queryTableDataInfo({ params, callback }: any, { call, put }: any) {
      const { code, data } = yield call(queryTableDataInfoReq, params);
      if (code === 200) {
        yield put({
          type: "save",
          payload: {
            dictInfo: data,
          },
        });
        // eslint-disable-next-line @typescript-eslint/no-unused-expressions
        callback && callback();
      }
    },
    //修改
    *updateDictionaryType({ params, callback }: any, { call }: any) {
      const { code } = yield call(updateDictionaryTypeReq, params);
      if (code === 200) {
        // eslint-disable-next-line @typescript-eslint/no-unused-expressions
        callback && callback();
      }
    },
    *refreshList({ params }: any, { call }: any) {
      const { code, msg } = yield call(refreshListReq, params);
      message[code === 200 ? "success" : "warning"](msg);
    },
  },
  // 同步存储
  reducers: {
    save(state: any, { payload }: any) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
